package com.koushare.utils;

import com.alibaba.excel.util.StringUtils;
import com.koushare.entity.BaseColleges;
import com.koushare.entity.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName CasReptileUtisl
 * @Description 爬虫工具类
 * @Author lidaofu
 * @Date 2021/3/19 13:07
 * @Version V1.0
 **/
@Slf4j
public class ReptileUtils {
    private final static String dn = "com|cn|org|com.cn|xyz|net|gg|gov.cn|love|edu.hk||hk";
    private final static Pattern pattern = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.(" + dn + ")");
    /**
     * 验证手机号码
     * <p>
     * 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
     * 联通号码段:130、131、132、136、185、186、145
     * 电信号码段:133、153、180、189
     *
     * @param cellphone
     * @return
     */
    public final static Pattern mobilePattern = Pattern.compile("(1[3-9]\\d{9})");
    /**
     * 验证固话号码
     *
     * @param telephone
     * @return
     */
    public final static Pattern phonePattern = Pattern.compile("(?:(0[0-9]{2,3}\\\\\\\\-?)?([2-9][0-9]{6,7})+(\\\\\\\\-[0-9]{1,4})?)|\\\" +\\n\" +\n" +
            "                \"        \\\"(?:(86-?)?(0[0-9]{2,4})?)");


    /**
     * 爬取工具 -表层加里层
     *
     * @param baseColleges
     * @return
     */
    public static List<Teacher> reptile(BaseColleges baseColleges) {
        List<Teacher> list = new ArrayList<>();
        Page page = RequestAndResponseTool.sendRequstAndGetResponse(baseColleges.getStartUrl());
        Set<String> links = PageParserTool.getLinks(page, baseColleges.getHrefCss());
        for (String link : links) {
            if (!link.contains("http")) {
                continue;
            }
            try {
                Teacher teacher = new Teacher();
                Page pageIndex = RequestAndResponseTool.sendRequstAndGetResponse(link);
                Elements selectName = PageParserTool.select(pageIndex, baseColleges.getNameCss());
                if (selectName != null) {
                    teacher.setName(selectName.text());
                    if (StringUtils.isEmpty(teacher.getName())) {
                        continue;
                    }
                    if (StringUtils.isNotBlank(baseColleges.getEmailCss())) {
                        Elements emailText = PageParserTool.select(pageIndex, baseColleges.getEmailCss());
                        if (emailText != null) {
                            if (baseColleges.getRexEmail() != null && baseColleges.getRexEmail()) {
                                Matcher matcher = pattern.matcher(emailText.text());
                                if (matcher.find()) {
                                    String result = matcher.group();
                                    teacher.setEmail(result);
                                }
                            } else {
                                teacher.setEmail(emailText.text());
                            }
                        }
                    }
//                    if (StringUtils.isNotBlank(baseColleges.getPhoneCss())) {
//                        Elements phoneText = PageParserTool.select(pageIndex, baseColleges.getPhoneCss());
//                        if (phoneText != null) {
//                            teacher.setPhone(phoneText.text());
//                        }
//                    }
                    if (StringUtils.isNotBlank(baseColleges.getPhoneCss())) {
                        Elements phoneText = PageParserTool.select(pageIndex, baseColleges.getPhoneCss());
                        if (phoneText != null) {
                            Matcher phoneMatcher = mobilePattern.matcher(phoneText.text());
                            if (phoneMatcher.find()) {
                                String result = phoneMatcher.group();
                                teacher.setPhone(result);

                            } else {
                                Matcher matcherRe = phonePattern.matcher(phoneText.text());
                                if (matcherRe.find()) {
                                    String result = matcherRe.group();
                                    teacher.setPhone(baseColleges.getArea()+"-"+result);
                                }
                            }
                        }
                    }
                    if (StringUtils.isNotBlank(baseColleges.getDirectionCss())) {
                        Elements directionText = PageParserTool.select(pageIndex, baseColleges.getDirectionCss());
                        if (directionText != null) {
                            teacher.setDirection(directionText.text());
                        }
                    }
                    teacher.setUniversity(baseColleges.getUniversity());
                    teacher.setColleges(baseColleges.getColleges());
                    teacher.setUrl(link);
                    list.add(teacher);
                }
            } catch (Exception e) {
                log.error("爬取教师错误：{}", link);
                log.error("异常原因：", e);
            }
        }

        return list;
    }


    /**
     * 爬取工具-表层 参考吉林大学物理学院
     *
     * @param baseColleges
     * @return
     */
    public static List<Teacher> reptileOutSider(BaseColleges baseColleges) {
        List<Teacher> list = new ArrayList<>();
        Page page = RequestAndResponseTool.sendRequstAndGetResponse(baseColleges.getStartUrl());
        Elements selectName = PageParserTool.select(page, baseColleges.getNameCss());
//        Set<String> links = PageParserTool.getLinks(page, baseColleges.getHrefCss());
        Elements group = PageParserTool.select(page, baseColleges.getHrefCss());
        for (Element element : group) {
            try {
                Teacher teacher = new Teacher();
                if (element != null) {
                    Elements name = PageParserTool.selectInElement(element, baseColleges.getNameCss());
                    teacher.setName(name.text());
//                    if (StringUtils.isEmpty(teacher.getName())) {
//
//                    }
                    if (StringUtils.isNotBlank(baseColleges.getEmailCss())) {
                        Elements emailText = PageParserTool.selectInElement(element, baseColleges.getEmailCss());
                        if (emailText != null) {
                            if (baseColleges.getRexEmail() != null && baseColleges.getRexEmail()) {
                                Matcher matcher = pattern.matcher(emailText.text());
                                if (matcher.find()) {
                                    String result = matcher.group();
                                    teacher.setEmail(result);
                                }
                            } else {
                                teacher.setEmail(emailText.text());
                            }
                        }
                    }
                    if (StringUtils.isNotBlank(baseColleges.getPhoneCss())) {
                        Elements phoneText = PageParserTool.selectInElement(element, baseColleges.getPhoneCss());
                        if (phoneText != null) {
                            teacher.setPhone(phoneText.text());
                        }
                    }
                    if (StringUtils.isNotBlank(baseColleges.getDirectionCss())) {
                        Elements directionText = PageParserTool.selectInElement(element, baseColleges.getDirectionCss());
                        if (directionText != null) {
                            teacher.setDirection(directionText.text());
                        }
                    }
                    teacher.setUniversity(baseColleges.getUniversity());
                    teacher.setColleges(baseColleges.getColleges());
                    teacher.setUrl(baseColleges.getStartUrl());
                    list.add(teacher);
                }
            } catch (Exception e) {
                log.error("爬取教师错误：{}", baseColleges.getStartUrl());
                log.error("异常原因：", e);
            }

        }
        return list;
    }

    /**
     * 爬取工具 -表层加里层
     *
     * @param baseColleges
     * @return
     */
    public static List<Teacher> reptileBoke(BaseColleges baseColleges) {
        List<Teacher> list = new ArrayList<>();
        Page page = RequestAndResponseTool.sendRequstAndGetResponse(baseColleges.getStartUrl());
        Set<String> links = PageParserTool.getLinks(page, baseColleges.getHrefCss());
        for (String link : links) {
            if (!link.contains("http")) {
                continue;
            }
            try {
                Teacher teacher = new Teacher();
                Page pageIndex = RequestAndResponseTool.sendRequstAndGetResponse(link);
                Elements selectName = PageParserTool.select(pageIndex, baseColleges.getNameCss());
                if (selectName != null) {
                    teacher.setName(selectName.text());
                    if (StringUtils.isEmpty(teacher.getName())) {
                        continue;
                    }
                    if (StringUtils.isNotBlank(baseColleges.getEmailCss())) {
                        Elements emailText = PageParserTool.select(pageIndex, baseColleges.getEmailCss());
                        if (emailText != null) {
                            if (baseColleges.getRexEmail() != null && baseColleges.getRexEmail()) {
                                Matcher matcher = pattern.matcher(emailText.text());
                                if (matcher.find()) {
                                    String result = matcher.group();
                                    teacher.setEmail(result);
                                }
                            } else {
                                teacher.setEmail(emailText.text());
                            }
                        }
                    }
//                    if (StringUtils.isNotBlank(baseColleges.getPhoneCss())) {
//                        Elements phoneText = PageParserTool.select(pageIndex, baseColleges.getPhoneCss());
//                        if (phoneText != null) {
//                            teacher.setPhone(phoneText.text());
//                        }
//                    }
                    if (StringUtils.isNotBlank(baseColleges.getPhoneCss())) {
                        Elements phoneText = PageParserTool.select(pageIndex, baseColleges.getPhoneCss());
                        if (phoneText != null) {
                            Matcher phoneMatcher = mobilePattern.matcher(phoneText.text());
                            if (phoneMatcher.find()) {
                                String result = phoneMatcher.group();
                                teacher.setPhone(result);

                            } else {
                                Matcher matcherRe = phonePattern.matcher(phoneText.text());
                                if (matcherRe.find()) {
                                    String result = matcherRe.group();
                                    teacher.setPhone(result);
                                }
                            }
                        }
                    }
                    if (StringUtils.isNotBlank(baseColleges.getDirectionCss())) {
                        Elements directionText = PageParserTool.select(pageIndex, baseColleges.getDirectionCss());
                        if (directionText != null) {
                            teacher.setDirection(directionText.text());
                        }
                    }
                    teacher.setUniversity(baseColleges.getUniversity());
                    teacher.setColleges(baseColleges.getColleges());
                    teacher.setUrl(link);
                    list.add(teacher);
                }
            } catch (Exception e) {
                log.error("爬取教师错误：{}", link);
                log.error("异常原因：", e);
            }
        }

        return list;
    }

    /**
     * 爬取工具 -表层加里层-苏州大学类，极为混乱
     *
     * @param baseColleges
     * @return
     */
    public static List<Teacher> reptileSZ(BaseColleges baseColleges) {
        List<Teacher> list = new ArrayList<>();
        Page page = RequestAndResponseTool.sendRequstAndGetResponse(baseColleges.getStartUrl());
        Set<String> links = PageParserTool.getLinks(page, baseColleges.getHrefCss());
        for (String link : links) {
            if (!link.contains("http")) {
                continue;
            }
            try {
                Teacher teacher = new Teacher();
                Page pageIndex = RequestAndResponseTool.sendRequstAndGetResponse(link);
                Elements selectName = PageParserTool.select(page, baseColleges.getNameCss());
                if (selectName != null) {
                    teacher.setName(selectName.text());
                    if (StringUtils.isEmpty(teacher.getName())) {
                        continue;
                    }
                    if (StringUtils.isNotBlank(baseColleges.getEmailCss())) {
                        Elements emailText = PageParserTool.select(pageIndex, baseColleges.getEmailCss());
                        if (emailText != null) {
                            if (baseColleges.getRexEmail() != null && baseColleges.getRexEmail()) {
                                Matcher matcher = pattern.matcher(emailText.text());
                                if (matcher.find()) {
                                    String result = matcher.group();
                                    teacher.setEmail(result);
                                }
                            } else {
                                teacher.setEmail(emailText.text());
                            }
                        }
                    }
//                    if (StringUtils.isNotBlank(baseColleges.getPhoneCss())) {
//                        Elements phoneText = PageParserTool.select(pageIndex, baseColleges.getPhoneCss());
//                        if (phoneText != null) {
//                            teacher.setPhone(phoneText.text());
//                        }
//                    }
                    if (StringUtils.isNotBlank(baseColleges.getPhoneCss())) {
                        Elements phoneText = PageParserTool.select(pageIndex, baseColleges.getPhoneCss());
                        if (phoneText != null) {
                            Matcher phoneMatcher = mobilePattern.matcher(phoneText.text());
                            if (phoneMatcher.find()) {
                                String result = phoneMatcher.group();
                                teacher.setPhone(result);

                            } else {
                                Matcher matcherRe = phonePattern.matcher(phoneText.text());
                                if (matcherRe.find()) {
                                    String result = matcherRe.group();
                                    teacher.setPhone(baseColleges.getArea()+"-"+result);
                                }
                            }
                        }
                    }
                    if (StringUtils.isNotBlank(baseColleges.getDirectionCss())) {
                        Elements directionText = PageParserTool.select(pageIndex, baseColleges.getDirectionCss());
                        if (directionText != null) {
                            teacher.setDirection(directionText.text());
                        }
                    }
                    teacher.setUniversity(baseColleges.getUniversity());
                    teacher.setColleges(baseColleges.getColleges());
                    teacher.setUrl(link);
                    list.add(teacher);
                }
            } catch (Exception e) {
                log.error("爬取教师错误：{}", link);
                log.error("异常原因：", e);
            }
        }

        return list;
    }


    /**
     * 爬取详情页工具
     *
     * @param sourceUrl
     * @param titleCss
     * @param contentCss
     * @param oldStr
     * @param newStr
     * @return
     */
    public static String reptilePage(String sourceUrl, String titleCss, String contentCss, String oldStr, String newStr) {
        try {
            Page pageIndex = RequestAndResponseTool.sendRequstAndGetResponse(sourceUrl);
            Elements selectTitle = PageParserTool.select(pageIndex, titleCss);
            String text = selectTitle.text();
            if (!StringUtils.isEmpty(text)) {
                Elements selectContent = PageParserTool.select(pageIndex, contentCss);
                if (!oldStr.equals("")) {
                    return selectContent.html().replace(oldStr, newStr);
                } else {
                    return selectContent.html();
                }
            }

        } catch (Exception e) {
            log.error("爬取文章错误：{}", sourceUrl);
            log.error("异常原因：", e);
        }
        return null;
    }

}
